package com.clkj.zhonghui.service.banner;

import com.clkj.zhonghui.dao.banner.BannerMapper;

import com.clkj.zhonghui.pojo.banner.Banner;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author：周佳润
 * @Date：2024/7/10 上午9:37
 */
@Service
public class BannerServiceImpl implements BannerService {

    @Autowired
    private BannerMapper bannerMapper;



    @Override
    public PageInfo listBanners(Integer page, Integer size, Integer bannerFlag) {
        PageHelper.startPage(page, size);
        Example example = new Example(Banner.class);
        Example.Criteria criteria = example.createCriteria();
        if (bannerFlag != null && !bannerFlag.equals(-1)) {
            criteria.andEqualTo("bannerFlag", bannerFlag);
        }
        return new PageInfo(bannerMapper.selectByExample(example));

    }

    @Override
    public void addBanner(Banner banner, String flag, String fileName) {
        if(fileName!=""){
            banner.setBannerName(fileName);
        }
        if("add".equals(flag)){
            bannerMapper.insertSelective(banner);
        }else{
            bannerMapper.updateByPrimaryKeySelective(banner);
        }
    }

    @Override
    public void deleteBanner(String ids) {
        List<Integer> idList = new ArrayList<>();
        String [] id = ids.split(",");
        for (String s : id) {
            idList.add(Integer.parseInt(s));
        }
        Example example = new Example(Banner.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("bannerId",idList);
        bannerMapper.deleteByExample(example);

    }

    @Override
    public void updateBanner(String ids, Integer bannerStatus) {
        List<Integer> idList = new ArrayList<>();
        String [] id = ids.split(",");
        for (String s : id) {
            idList.add(Integer.parseInt(s));
        }
        Example example = new Example(Banner.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("bannerId",idList);
        Banner banner = new Banner();
        banner.setBannerStatus(bannerStatus);
        bannerMapper.updateByExampleSelective(banner,example);
    }
}
